package pack0901;

public class DemoA {
    private int i = 1;

    //同步块
    public void a() {
        synchronized (this) {
            i++;
        }
    }

    public void b() {
        System.out.println(i);
    }

    public static void main(String[] args) {
        DemoA da = new DemoA();
        DemoA1 da1 = new DemoA1(da);
        DemoA2 da2 = new DemoA2(da);
        da1.start();
        da2.start();
        try {
            Thread.sleep(1000);
        } catch (Exception e) {
        }
        da.b();
    }
}
class DemoA1 extends Thread {
    private DemoA da;

    public DemoA1(DemoA da) {
                this.da = da;
            }
    @Override
    public void run() {
        System.out.println("aaa");
        for (int i = 0; i<10; i++){
            da.a();
            System.out.print("a:");
            da.b();
        }
    }
}
class DemoA2 extends Thread {
    private DemoA da;

    public DemoA2(DemoA da) {
        this.da = da;
    }

    @Override
    public void run() {
        System.out.println("bbb");
        for (int i = 0; i < 10; i++) {
            da.a();
            System.out.print("b:");
            da.b();
        }
    }
}
